package com.company;

import com.company.proxy.type3.ProxyByCglib;
import com.company.proxy.type3.RealSubject;
import org.springframework.cglib.proxy.Enhancer;

/**
 * CGLib动态代理模式测试代码
 */
public class TestForCglib {

    public static void main(String[] args) {
        //直接调用真实对象的方法
        RealSubject realObj = new RealSubject();
        realObj.method1();
        System.out.println("----------");
        realObj.method2();

        System.out.println("=========================");

        //调用动态代理对象的方法
        //被代理的对象
        RealSubject delegate = new RealSubject();
        ProxyByCglib proxyInterceptor = new ProxyByCglib(delegate);
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(delegate.getClass());
        enhancer.setCallback(proxyInterceptor);

        //动态代理类
        RealSubject cglibProxy = (RealSubject)enhancer.create();

        //代理方式执行相关方法
        cglibProxy.method1();
        System.out.println("----------");
        cglibProxy.method2();
    }
}
